/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.index;

import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.*;

/**
 * @author bduphoff
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class IndexBuildInput {

    boolean buildInPlace = false;

    String[] dataFiles = null;

    int pageSize = -1;

    int month = -1;

    int day = -1;

    int year = -1;

    int run = -1;

    int offset = -1;

    long cacheSize = -1;

    String statDB = "";

    String statHost = "";

    String statUser = "";

    String statPassword = "";

    String metaDB = "";

    String metaHost = "";

    String metaUser = "";

    String metaPassword = "";

    String tempDB = "";

    int dataType = -1;

    String toolName = "";

    String sensorName = "";

    int sensorID = -1;

    String globalDir = "";

    String localDir = "";

    String sharedDir = "";

    IndexField[] fields = null;

    //String extension = "";

    String[] nodes = null; // Node used to build the indices

    long minTime = -1;

    long maxTime = -1;

    long bytes = 0;

    long records = 0;

    public IndexBuildInput() {

    }

    public static IndexBuildInput processParms(String inputFilePath) {
        try {
            IndexBuildInput input = new IndexBuildInput();
            Vector<String> nodes = new Vector<String>();
            Vector<IndexField> fields = new Vector<IndexField>();
            Vector<String> dataFiles = new Vector<String>();

            LineNumberReader in = new LineNumberReader(new FileReader(inputFilePath));
            String line = in.readLine();
            while (line != null) {
                String[] tokens = line.split(" ");
                if (tokens[0].equals("db")) {
                    IndexField f = new IndexField();
                    f.dbPath = tokens[1];
                    f.dbName = tokens[2];
                    f.id = Integer.parseInt(tokens[3]);
                    fields.addElement(f);
                } else if (tokens[0].equals("pageSize")) {
                    input.pageSize = Integer.parseInt(tokens[1]);
                } else if (tokens[0].equals("month")) {
                    input.month = Integer.parseInt(tokens[1]);
                } else if (tokens[0].equals("day")) {
                    input.day = Integer.parseInt(tokens[1]);
                } else if (tokens[0].equals("year")) {
                    input.year = Integer.parseInt(tokens[1]);
                } else if (tokens[0].equals("run")) {
                    input.run = Integer.parseInt(tokens[1]);
                } else if (tokens[0].equals("cacheSize")) {
                    input.cacheSize = Integer.parseInt(tokens[1]);
                } else if (tokens[0].equals("dataFile")) {
                    dataFiles.addElement(tokens[1]);
                } else if (tokens[0].equals("dataType")) {
                    input.dataType = Integer.parseInt(tokens[1]);
                } else if (tokens[0].equals("tempDB")) {
                    input.tempDB = tokens[1];
                    // cout << "tempDB='" << input.tempDB << "'" << endl;
                } else if (tokens[0].equals("statDB")) {
                    String host = tokens[1];
                    String db = tokens[2];
                    String user = tokens[3];
                    String password = tokens[4];
                    input.statHost = host;
                    input.statDB = db;
                    input.statUser = user;
                    input.statPassword = password;
                } else if (tokens[0].equals("metaDB")) {
                    String host = tokens[1];
                    String db = tokens[2];
                    String user = tokens[3];
                    String password = tokens[4];
                    input.metaHost = host;
                    input.metaDB = db;
                    input.metaUser = user;
                    input.metaPassword = password;
                } else if (tokens[0].equals("node")) {
                    nodes.addElement(tokens[1]);
                } else if (tokens[0].equals("minTime")) {
                    input.minTime = Long.parseLong(tokens[1]);
                } else if (tokens[0].equals("maxTime")) {
                    input.maxTime = Long.parseLong(tokens[1]);
                } else if (tokens[0].equals("bytes")) {
                    input.bytes = Long.parseLong(tokens[1]);
                } else if (tokens[0].equals("records")) {
                    input.records = Long.parseLong(tokens[1]);
                } else if (tokens[0].equals("globalDir")) {
                    input.globalDir = tokens[1];
                } else if (tokens[0].equals("sharedDir")) {
                    input.sharedDir = tokens[1];
                } else if (tokens[0].equals("localDir")) {
                    input.localDir = tokens[1];
                } else if (tokens[0].equals("toolName")) {
                    input.toolName = tokens[1];
                } else if (tokens[0].equals("sensorName")) {
                    input.sensorName = tokens[1];
                } else if (tokens[0].equals("sensorID")) {
                    input.sensorID = Integer.parseInt(tokens[1]);
                } else if (tokens[0].equals("extension")) {
                    //input.extension = tokens[1];
                } else if (tokens[0].equals("buildInPlace")) {
                    if (tokens[1].equals("true"))
                        input.buildInPlace = true;
                    //System.out.println("BUILD IN PLACE: " + input.buildInPlace);
                }
                line = in.readLine();
            }
            in.close();
            input.nodes = new String[nodes.size()];
            for (int i = 0; i < input.nodes.length; i++) {
                input.nodes[i] = nodes.elementAt(i);
            }
            input.fields = new IndexField[fields.size()];
            for (int i = 0; i < input.fields.length; i++) {
                input.fields[i] = fields.elementAt(i);
            }
            input.dataFiles = new String[dataFiles.size()];
            for (int i = 0; i < input.dataFiles.length; i++) {
                input.dataFiles[i] = dataFiles.elementAt(i);
            }

            return input;
        } catch (Exception E) {
            E.printStackTrace();
        }
        return null;
    }
}
